﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Windows.Forms;

namespace Warehouse
{
    class DBConnect
    {
        private string _server;
        private string _database;
        private string _uid;
        private string _password;
        private string _connectionString;
        private MySqlConnection _connection;

        public DBConnect(String _ip, String _warehouse)
        {
            _server = _ip;
            _database = _warehouse;
            Initialize();
        }
        private void Initialize()
        {
            _uid = "tulip";
            _password = "weareone";
            _connectionString = "SERVER=" + _server + ";" + "DATABASE=" +
            _database + ";" + "UID=" + _uid + ";" + "PASSWORD=" + _password + ";";
            _connection = new MySqlConnection(_connectionString);
            OpenConnection();
            CloseConnection();
        }
        public MySqlConnection OpenConnection()
        {
            try
            {
                _connection.Open();
                return _connection;
            }
            catch (MySqlException ex)
            {
                switch (ex.Number)
                {
                    case 0:
                        MessageBox.Show("Cannot connect to server.  Contact administrator");
                        break;

                    case 1045:
                        MessageBox.Show("Invalid username/password, please try again");
                        break;
                }
                return _connection;
            }
        }
        public bool CloseConnection()
        {
            try
            {
                _connection.Close();
                return true;
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }

        public string getDBString() { return _connectionString; }
        public MySqlConnection getConnection() { return _connection; }
    }
}
